package com.wangxl.rabbitmqdemo.producer;

import com.wangxl.rabbitmqdemo.enumtype.RoutingKeyEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.UUID;

/**
 * @Author: wangxl
 * @Date: 2019/3/23 14:19
 */
@Component
@Slf4j
public class SimpleMessageSender {

    @Resource
    RabbitTemplate rabbitTemplate;

//    @PostConstruct
//    public void init(){
//        /**
//         * 配置消息发送回调操作
//         */
//        rabbitTemplate.setConfirmCallback((correlationData,ack,cause)->{
//            if(ack){
//                log.info("send message success----{}",correlationData.getReturnedMessage());
//            }else{
//                log.info("send message fail----{}",cause);
//            }
//        });
//        rabbitTemplate.setReturnCallback((message,replyCode,replyText,exchange,routingKey)->{
//            log.info("send message fail----{}",message.getMessageProperties().getMessageId());
//        });
//    }

    public void sendSimpleMessage(String messageContent){

        String uuid = UUID.randomUUID().toString();
        log.info("send simple message---{}--uuid---{}",messageContent,uuid);
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(uuid);
        Object res = rabbitTemplate.convertSendAndReceive("simpleExchange", RoutingKeyEnum.SIMPLE_KEY.getRoutingKey(), messageContent, correlationData);

        log.info("receive message----{}----uuid---{}",res,uuid);
    }
}
